package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据pid查询对应的分类信息,由于pid可能对应多个分类,返回结果是集合
     * @param pid
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam("pid") Long pid){
        List<Category> categories = categoryService.queryByParentId(pid);
        if (categories != null) {
            return ResponseEntity.ok(categories);//表达，返回所有的分类数据，并且返回状态码200
        }
        //没有数据就返回204
        return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *
     * 根据品牌id查询对应的分类对象
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid")Long bid){
        List<Category> categories = this.categoryService.queryByBrandId(bid);
        if (categories != null && 0!=categories.size()) {
            return ResponseEntity.ok(categories);
        }

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();//204
    }
}
